/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @author: jin Chen
 * @date: Jun 17, 2003
 * @version: $VERSION$
 */
package edu.psu.geovista.app.plot.axis;

import edu.psu.geovista.app.plot.ruler.DfRuler;

import java.awt.*;
import java.util.Collection;
import java.util.Iterator;

public class SlideRuler extends DfRuler{
    public static final String ET="EastTop";
    public static final String EB="EastBottom";
    public static final String WT="WestTop";
    public static final String WB="WestBottom";
    ThumbList thumbs;


    public SlideRuler() {
        this(200,edu.psu.geovista.app.plot.ruler.Ruler.VERTICAL ,new Point(0,0),new Range(100,0));
       // this.initial() ;
    }

    public SlideRuler(int orient, int length, Point op, Range tickRange) {
        super(orient, length, op, tickRange);
       // this.initial() ;

    }

    /*******************************************************************************************************
     *                GUI
     *******************************************************************************************************/

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        this.paintThumb(g);

    }
    private void paintThumb(Graphics g){
       if (thumbs!=null&&thumbs.size() >0){
           Collection tbs=this.thumbs.values() ;
           Iterator iter=tbs.iterator() ;
           while(iter.hasNext() ){
                Thumb tb=(Thumb)iter.next() ;
                //reset location of thumb
                int y=this.getGraphicValueOf(tb.getValue() ) ;
                tb.setPt(this.getLocation().x, y);
                //tb.setPt(this.getLocation().x, tb.getPt().y);
                tb.paintComponent(g);

           }
       }
    }
    /*******************************************************************************************************
     *                operation
     *******************************************************************************************************/
    /**
     *  set a thumb to locate on the point with given value   on the axis
     */
    public void setThumbValue(Thumb tb, float value) {
        tb.setValue(value);
        //Not need. paintThumb will recalculate thumb's location
        //int gv=this.getGraphicValueOf(value);//graphic locatoin
        //tb.moveTo(gv);
    }
    /**
     *  set a thumb to locate on the point with given value   on the axis

    public void setThumbValue(String id, float value) {

        Thumb tb=this.getThumb(id);
        this.setThumbValue(tb,value);
    }



    /*
    public void setThumbGraphicLoc(Thumb tb, Point p) {
        tb.setLocation(p);
    }    */
     public Thumb getThumb(Point p){
       Collection tbs=this.thumbs.values() ;
       Iterator iter=tbs.iterator() ;
       while(iter.hasNext() ){
            Thumb tb=(Thumb)iter.next() ;
            if (tb.contains(p)){
                return tb;
            }

       }
       return null;
    }
    public Thumb getThumb(int x, int y){
       Collection tbs=this.thumbs.values() ;
       Iterator iter=tbs.iterator() ;
       while(iter.hasNext() ){
            Thumb tb=(Thumb)iter.next() ;
            if (tb.contains(x,y)){
                return tb;
            }

       }
       return null;
    }
     public Thumb getThumb(String name){
        return (Thumb)this.thumbs.get(name);
     }



    /*******************************************************************************************************
     *                properties
     *******************************************************************************************************/
    public ThumbList getThumbs() {
        return thumbs;
    }

    public void setThumbs(ThumbList thumbs) {
        this.thumbs = thumbs;
    }


    public void setLength(int length) {
        super.setLength(length);
        //this.setThumbsDefaultValue() ;
        //this.setAllThumbValues() ;


    }
    public void setLocation(Point location) {
        super.setLocation(location);
        //this.setThumbsDefaultValue() ;
    }
}






  /**
     *  set a thumb to locate on the point with given value   on the axis

    public void setThumbLoc(String id, Point loc) {

        Thumb tb=this.getThumb(id);
        if (this.getOrient() ==Ruler.HORIZONTAL ){
            tb.moveTo(loc.x);
        }
        else if (this.getOrient() ==Ruler.VERTICAL ){
            tb.moveTo(loc.y);
        }
    }




    public void addThumb(Thumb tb, String name) {
        tb.setName(name);
        this.thumbs.put(name, tb);
    }
    public void addThumb(Thumb tb) {
        this.thumbs.put(tb.getName(), tb);
    }*/
/*
private void calculateThumbLocation(){
        if (thumbs!=null){
        Collection tbs=this.thumbs.values() ;
        Iterator iter=tbs.iterator() ;
        while(iter.hasNext() ){
            Thumb tb =(Thumb)iter.next() ;
            float value=tb.getValue() ;
            int gv=this.getGraphicValueOf(value);//graphic locatoin
            tb.moveTo(gv);


        }
    }*/


    /**
     *  set values
     /
    public void setAllThumbValues() {
        if (thumbs!=null){
        Collection tbs=this.thumbs.values() ;
        Iterator iter=tbs.iterator() ;
        while(iter.hasNext() ){
            Thumb tb =(Thumb)iter.next() ;
            this.setThumbValue(tb);

        }
        }
    }

    private void setThumbValue(Thumb tb) {
        float value=tb.getValue() ;
        if(value<this.getRange().getMin() ){
            value=this.getRange().getMin();
        }
        else if (value>this.getRange().getMax() ){
            value=this.getRange().getMax();
        }
        this.setThumbValue(tb,value);
        //Point pt= tb.getPt() ;
        //tb.setValue( this.getValueOf(pt.y));
    }

 //All ranges' value reference to axis's ranges
    public Extreme getLineShowRange() {
        return westShowRange;
    }

    public void setLineShowRange(Extreme lineShowRange) {
        this.westShowRange = lineShowRange;
        this.setThumbValue((Thumb)thumbs.get(ET),this.westShowRange.getMax() );
        this.setThumbValue((Thumb)thumbs.get(EB),this.westShowRange.getMin() );
    }

    public Extreme getAxisShowRange() {
        return eastShowRange;
    }

    public void setAxisShowRange(Extreme axisShowRange) {
        this.eastShowRange = axisShowRange;
        this.setThumbValue((Thumb)thumbs.get(WT),this.eastShowRange.getMax() );
        this.setThumbValue((Thumb)thumbs.get(WB),this.eastShowRange.getMin() );
    }
    public void setTickRange(Extreme range) {
        super.setTickRange(range);
        //this.setThumbsDefaultValue() ;
        //this.setAllThumbValues() ;
    }      */

 /*******************************************************************************************************
     *                initial default values
     *****************************************************************************************************
    private void initial(){
            this.thumbs=new Hashtable();
            this.createThumbs() ;
            this.setThumbsDefaultValue() ;
    }
    private void setThumbsDefaultValue(){
       if (this.thumbs!=null&&this.tickRange !=null){
           //east thumb facing east, represent data on west side of ruler
           this.setThumbValue((Thumb)thumbs.get(ET),this.getTickRange().getMax() );
           this.setThumbValue((Thumb)thumbs.get(EB),this.getTickRange().getMin() );
           this.setThumbValue((Thumb)thumbs.get(WT),this.getTickRange().getMax() );
           this.setThumbValue((Thumb)thumbs.get(WB),this.getTickRange().getMin() );
       }
    }
    private void createThumbs(){

        Thumb et,eb,wt,wb;
        //Extreme r=this.getRange() ;
        et=new Thumb(Thumb.EAST,Color.green,ET);

        eb=new Thumb(Thumb.EAST,Color.green,EB);
        //this.setThumbValue(eb,eastRange.getMin() );

        wt=new Thumb(Thumb.WEST,Color.blue,WT);
       // this.setThumbValue(wt,westRange.getMax() );
        wb=new Thumb(Thumb.WEST,Color.blue,WB);
        //this.setThumbValue(wb,westRange.getMin() );

        this.addThumb(et);
        this.addThumb(eb);
        this.addThumb(wt);
        this.addThumb(wb);
    }
    /*private void reset(){
        Extreme dataRange=this.getRange();
            if(dataRange!=null){
                float max=dataRange.getMax() ;
                float min=dataRange.getMin();
                this.lineShowRange =new Extreme( max,min);
                this.axisShowRange =new Extreme( max,min);
                this.setThumbsDefaultValue() ;
            }
    }     */


/*
    protected void setup(BasicAxis ax){
        super.setup(ax);
        axis=ax;
        ax.addMouseListener(this);
        ax.addMouseMotionListener(this);
        if (thumbs==null){ //this is initial execution
            this.initial() ;
        }
        //installUI(ax);//temperal, should be called from setUI()
     }      */